package com.kj.xuyuan.util;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import com.kj.xuyuan.R;
import com.kj.xuyuan.adapter.PhotoAdapter;
import com.kj.xuyuan.base.BaseActivity;
import java.util.ArrayList;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class PhotoViewActivity extends BaseActivity {

    @BindView(R.id.rv_photo)
    RecyclerView rvPhoto;

    private String url;
    private ArrayList<String> urls;
    private int position;
    private PhotoAdapter adapter;

    public static void actionStar(Context context, String url,int position) {
        Intent intent = new Intent(context, PhotoViewActivity.class);
        intent.putExtra("url", url);
        intent.putExtra("position", position);
        context.startActivity(intent);
    }

    public static void actionStar(Context context, ArrayList<String> urls,int position) {
        Intent intent = new Intent(context, PhotoViewActivity.class);
        intent.putStringArrayListExtra("urls", urls);
        intent.putExtra("position", position);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {

    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_photo_view;
    }

    @Override
    protected void initView() {
        urls = getIntent().getStringArrayListExtra("urls");
        position = getIntent().getIntExtra("position",0);
        url = getIntent().getStringExtra("url");

        if(urls == null){
            urls = new ArrayList<>();
            String[] str = url.split(",");
            LogUtils.e(TAG,"str ="+str.length);
            for(int i = 0;i<str.length;i++){
                urls.add(str[i]);
                LogUtils.e(TAG,"url ="+str[i]);
            }
        }

        initRecyclerView();
    }

    private void initRecyclerView() {
        rvPhoto.setLayoutManager(new LinearLayoutManager(context,RecyclerView.HORIZONTAL,false));
        adapter = new PhotoAdapter();
        rvPhoto.setAdapter(adapter);

        PagerSnapHelper snapHelper = new PagerSnapHelper();
        snapHelper.attachToRecyclerView(rvPhoto);

        adapter.setNewData(urls);

        rvPhoto.scrollToPosition(position);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getTitleBarType() {
        return -1;
    }

}